/**
 * Created by gongxun on 17/1/19.
 */

var NnTableCancelLayer = GameCCSBase.extend({
    Panel_center : null,
    mCancelJu : null,
    mShengqingLogicSeat : null,

    Text_shengqing : null,
    Text_nick0 : null,
    Text_nick1 : null,
    Text_nick2 : null,
    Text_nick3 : null,
    Text_select0 : null,
    Text_select1 : null,
    Text_select2 : null,
    Text_select3 : null,
    mLeftTime : null,
    Text_timeLeft : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/NnTableCancleJu.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");
        this.mCancelJu = null;

        mo.seekWidgetByName(this.ccsNode, "Button_cancel").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.sendCancelTableRefuse();
                this.setTouchButton();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.sendCancelTableAgree();
                this.setTouchButton();
            }
        }, this);

        this.loadUI();

        this.setVisible(false);
    },

    onEnter : function(){
        this._super();
        this.showAction(this.Panel_center);

        mo.eventHelper.add("CancleJuRequestnn", this, function(cancleJu){
            this.updateCancelJuInfo(cancleJu);
        });

        this.mLeftTime = 0;
        this.schedule(function(dt){
            this.mLeftTime -= dt;
            if (this.mLeftTime < 0){
                this.mLeftTime = 0;
            }
            mo.gameHelper.setTextString(this.Text_timeLeft, parseInt(this.mLeftTime) + "");
        }, this);
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    loadUI: function () {
        this.Text_shengqing = mo.seekWidgetByName(this.ccsNode, "Text_shengqing");
        this.Text_nick0 = mo.seekWidgetByName(this.ccsNode, "Text_nick0");
        this.Text_nick1 = mo.seekWidgetByName(this.ccsNode, "Text_nick1");
        this.Text_nick2 = mo.seekWidgetByName(this.ccsNode, "Text_nick2");
        this.Text_nick3 = mo.seekWidgetByName(this.ccsNode, "Text_nick3");
        this.Text_select0 = mo.seekWidgetByName(this.ccsNode, "Text_select0");
        this.Text_select1 = mo.seekWidgetByName(this.ccsNode, "Text_select1");
        this.Text_select2 = mo.seekWidgetByName(this.ccsNode, "Text_select2");
        this.Text_select3 = mo.seekWidgetByName(this.ccsNode, "Text_select3");

        this.Text_timeLeft = mo.seekWidgetByName(this.ccsNode, "Text_timeLeft");
    },

    setShowThis : function(){
        mo.seekWidgetByName(this.ccsNode, "Button_cancel").setTouchEnabled(true);
        mo.seekWidgetByName(this.ccsNode, "Button_ok").setTouchEnabled(true);

        this.stopAllActions();
        this.setVisible(true);
        this.mLeftTime = 120;
    },

    setCloseThis : function(){
        this.setTouchButton();
        this.mCancelJu = null;
        this.mShengqingLogicSeat = null;

        this.stopAllActions();
        this.runAction(cc.sequence(
            cc.delayTime(1),
            cc.hide()
        ));
    },

    resetCancelJu : function(){
        this.stopAllActions();
        this.setTouchButton();
        this.mCancelJu = null;
        this.mShengqingLogicSeat = null;
        this.setVisible(false);
    },

    setTouchButton : function(){
        mo.seekWidgetByName(this.ccsNode, "Button_cancel").setTouchEnabled(false);
        mo.seekWidgetByName(this.ccsNode, "Button_ok").setTouchEnabled(false);
    },

    updateCancelJuInfo : function(cancleJu){
        if (this.mCancelJu == null){
            this.mCancelJu = cancleJu;

            if (cancleJu.uid1 == 111){
                this.mShengqingLogicSeat = 1;
            }else if (cancleJu.uid2 == 111){
                this.mShengqingLogicSeat = 2;
            }else if (cancleJu.uid3 == 111){
                this.mShengqingLogicSeat = 3;
            }else if (cancleJu.uid4 == 111){
                this.mShengqingLogicSeat = 4;
            }else if (cancleJu.uid5 == 111){
                this.mShengqingLogicSeat = 5;
            }

            if (this.mShengqingLogicSeat != null){
                this.setShowThis();

                if (this.mShengqingLogicSeat == mo.nnTableStatus.mTableStatus.MySeat){
                    this.setTouchButton();
                }
            }else{
                return;
            }
        }else{
            this.mCancelJu = cancleJu;
        }

        var getNickForLogicSeat = function(logicSeat){
            var nick = null;
            if (mo.nnTableStatus.mTableStatus["SeatUser" + logicSeat]){
                nick = mo.nnTableStatus.mTableStatus["SeatUser" + logicSeat].user_info.nickname;
            }
            return nick;
        };


        //申请人
        var shenqingStr = "玩家[" + getNickForLogicSeat(this.mShengqingLogicSeat) + "]申请解散房间!";
        mo.gameHelper.setTextString(this.Text_shengqing, shenqingStr);

        var userIndexs = [];
        for (var i = 1; i <= 5; i++) {
            if (i == this.mShengqingLogicSeat) {
                continue;
            }
            if (getNickForLogicSeat(i) != null){
                userIndexs.push(i);
            }
        }

        var useId = 0;
        for (var i = 1; i <= 5; i++){
            if (i == this.mShengqingLogicSeat){
                continue;
            }

            var index = 0;
            if (userIndexs.length > 0){
                index = userIndexs.shift();
                this["Text_nick" + useId].setVisible(true);
                this["Text_select" + useId].setVisible(true);

                //nick
                mo.gameHelper.setTextString(this["Text_nick" + useId], getNickForLogicSeat(index));

                //select
                var code = cancleJu["uid" + index];
                var Text_select = this["Text_select" + useId];
                if (code == 0){
                    mo.gameHelper.setTextString(Text_select, "等待选择");
                    Text_select.setTextColor(cc.color(0, 71, 0));
                }else if (code == 111){
                    mo.gameHelper.setTextString(Text_select, "同意");
                    Text_select.setTextColor(cc.color(0, 71, 0));
                }else if (code == 222){
                    mo.gameHelper.setTextString(Text_select, "拒绝");
                    Text_select.setTextColor(cc.color(120, 0, 0));
                    this.setCloseThis();
                    return;
                }
            }else{
                this["Text_nick" + useId].setVisible(false);
                this["Text_select" + useId].setVisible(false);
            }

            useId++;
        }
    },

    sendCancelTableAgree : function(){
        var code = 111;
        var myLogicSeat = mo.nnTableStatus.mTableStatus.MySeat;
        mo.gameMsgManager.sendCancleJuRequestnn(myLogicSeat, code);
    },

    sendCancelTableRefuse : function(){
        var code = 222;
        var myLogicSeat = mo.nnTableStatus.mTableStatus.MySeat;
        mo.gameMsgManager.sendCancleJuRequestnn(myLogicSeat, code);
    }
});